package com.zhouyu.fileSync.FScommon.commonUtils;

import com.zhouyu.fileSync.FScommon.annotations.RpcService;
import org.reflections.Reflections;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Set;

public class ServerMapUtils {

    public static HashMap<String, Object> getServiceMap(String rootPath) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Reflections rf = new Reflections(rootPath);
        Set<Class<?>> set = rf.getTypesAnnotatedWith(RpcService.class);
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        for (Class<?> aClass : set) {
            Object bean = aClass.getDeclaredConstructor().newInstance();
            //获取接口的名字，需要代理的rpc接口必须为第一个

            resultMap.put(aClass.getInterfaces()[0].getName(), bean);
        }
        return resultMap;
    }
}
